package com.fly.clientConfig.tank;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class HelloTank {

	@Autowired
	private RedisTemplate<String, String> redis;

	@Scheduled(cron = "0/10 * * * * ? ")
	public void tank() {

		// 先判断redis中是否有锁记录,如果能设值成功,代表拿到锁，不能设值成功就是锁还没释放
		if (redis.opsForValue().setIfAbsent("HelloTank", "HelloTank")) {
			// 设值成功后,设置锁超时时间 (我这里是9分钟)
			redis.expire("HelloTank", 9, TimeUnit.SECONDS);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
			Date now = new Date();
			String format = sdf.format(now);
			System.err.println("hello:" + format);
		}
	}

}
